<?php
/**
 * 
 * Enter description : Danh bạ người gửi nhận thư Lop này thao tac voi database
 * @author admin888 <doan.hut@gmail.com>
 * Date /17/06/2011
 */
class Model_Lienhe extends Zend_Db_Table
{
	protected $_nlh = "nhom_lien_he";
	protected $_lh  = 'lien_he';
	
	public function __construct()
	{
	  $this->_db =  Zend_Registry::get('connectDb');
	}
 	
 	/**
	 * admin888 
	 * Enter description :thêm mới nhom liên hệ
	 * @param unknown_type $data
	 */
	public function themmoinhomlienhe($data)
	{
		$date = date('m-d-Y H:i:s');
		$arr = array(
				'uid'		  => $data['iddn'],
				'ten_nhom_lh' =>$data['tenlh'],
				'ngay_tao'	  =>$date,
				'ngay_sua'	  =>$date	
		);
	 $query = $this->_db->insert($this->_nlh, $arr);
	 $id = $this->_db->lastInsertId($this->_nlh,'id_nlh');
	return $id;
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : Lấy toàn bộ tên nhóm liên hệ 
	 * @param unknown_type $uid
	 */
	public function getnhomluid($uid)
	{
		$sql = $this->_db->select()
						 ->from($this->_nlh )
						 ->where('uid = ?',$uid)
						 ->order('id_nlh DESC ');
		$query = $this->_db->fetchAll($sql);
	 return  $query;					 	 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here :update vào db thong tin nhóm lien he
	 * @param unknown_type $data
	 */
	public function getupdatenhomlh($data)
	{
		$date = date('m-d-Y H:i:s');
		$arr = array(
				'ten_nhom_lh' 			=> $data['tennhom'], 			
				'ngay_sua' 			    => $date
							
		);
		$where = "id_nlh =".$data['idnhom'];
		$query = $this->_db->update($this->_nlh,$arr,$where);
	return $query;	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function deletenhomlh($data)
	{
		$where  = "id_nlh = ".$data['idnlh'];
		$xoa = $this->_db->delete($this->_nlh,$where);
	}
	/**
	 * admin888 <paventt@gmail.com>
	 * Enter description :Lấy toàn bộ lien hệ theo id
	 * @param unknown_type $uid
	 */
	public  function getalllienhe($uid)
	{
		$sql = $this->_db->select()
						 ->from($this->_lh)
						 ->joinLeft($this->_nlh, $this->_lh.'.id_nlh = '.$this->_nlh.'.id_nlh','ten_nhom_lh')
						 ->where($this->_lh.'.uid = ?',$uid)
						 ->order('idlh  DESC');
			$query = $this->_db->fetchAll($sql);
		return $query;				 
				 
							
	}
	/**
	 * admin888 <paventt@gmail.com>
	 * Enter description :Lấy toàn bộ lien hệ theo id và sử dụng =1
	 * @param unknown_type $uid
	 */
	public  function getalllienhesudung($uid)
	{
		$sql = $this->_db->select()
						 ->from($this->_lh)
						 ->joinLeft($this->_nlh, $this->_lh.'.id_nlh = '.$this->_nlh.'.id_nlh','ten_nhom_lh')
						 ->where($this->_lh.'.uid = ?',$uid)
						 ->where($this->_lh.'.su_dung = ?',1)
						 ->order('idlh  DESC');
			$query = $this->_db->fetchAll($sql);
		return $query;				 
				 
							
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : lấy các nhóm liên hệ theo iddn 
	 * @param unknown_type $data
	 */
	public function getallnhomlhtheoiddn($data)
	{
		$sql = $this->_db->select()
						 ->from($this->_nlh)
						 ->where('uid = ?',$data['iddn'])
						 ->order('id_nlh DESC');
		$query = $this->_db->fetchAll($sql);
	 return  $query;					 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here : thêm mới liên hệ
	 * @param unknown_type $data
	 */
	public  function addlienhe($data)
	{
		$date = date('m-d-Y H:i:s');
		$arr = array(
			'id_nlh' 		=> $data['id_nhomlh'],
			'uid'			=> $data['iddn'],
		    'tenlh'			=>$data['tennguoilienhe'],
			'gio-tinh'		=>$data['gioitinh'],
			'ngay_sinh'		=>$data['ngaysinhnguoilienhe'],
			'email'			=>$data['emailnguoilienhe'],
			'dien_thoai'	=>$data['dienthoainguoilienhe'],
			'dia_chi'		=>$data['diachinguoilienhe'],
			'chuc_vu'		=>$data['chucvunguoilienhe'],
			'cong_ty'		=>$data['congtynguoilienhe'],
			'ghi_chu'		=>$data['ghichunguoilienhe'],
			'web'		    =>$data['websitenguoilienhe'],
			'flag'			=>$data['danhdau'],	
			'ngay_tao'	    =>$date,
			'ngay_sua'	    =>$date,
		
		);
		$query = $this->_db->insert($this->_lh, $arr);
	return $query;	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function getupdatelienhe($data)
	{
		$date = date('m-d-Y H:i:s');
		$arr = array(
			'su_dung'   => $data['thamso'],
			'ngay_sua'	=>$date
		);
	  $where = 'idlh = '.$data['idlh'];	
	  $query = $this->_db->update($this->_lh, $arr,$where)	;
	 return $query; 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :xoa 1 liên hệ 
	 * @param unknown_type $data
	 */
	public function xoaonelienhe($data)
	{
		$where = 'idlh = '.$data['idlh'];
		$query = $this->_db->delete($this->_lh,$where);
		return $query;
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : lay du lieu cua lien he
	 * @param unknown_type $data
	 */
	public function getlienheid($data)
	{
		$sql = $this->_db->select()
						 -> from($this->_lh)
						 ->join($this->_nlh, $this->_lh.'.id_nlh = '.$this->_nlh.'.id_nlh','ten_nhom_lh')
						 ->where($this->_lh.'.idlh = ?',$data['idlh']);
		$query = $this->_db->fetchRow($sql);
	return $query;				 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : update lien hệ
	 * @param unknown_type $data
	 */
	public function updatelienhe($data)
	{
		$date = date('m-d-Y H:i:s');
		$arr = array(
		    'tenlh'			=>$data['tennguoilienhesua'],
			'gio-tinh'		=>$data['gioitinhsua'],
			'ngay_sinh'		=>$data['ngaysinhnguoilienhesua'],
			'email'			=>$data['emailnguoilienhesua'],
			'dien_thoai'	=>$data['dienthoainguoilienhesua'],
			'dia_chi'		=>$data['diachinguoilienhesua'],
			'chuc_vu'		=>$data['chucvunguoilienhesua'],
			'cong_ty'		=>$data['congtynguoilienhesua'],
			'ghi_chu'		=>$data['ghichunguoilienhesua'],
			'web'		    =>$data['websitenguoilienhesua'],
			'flag'			=>$data['danhdaulh'], 
			'ngay_sua'	    =>$date
		
		);
		$where = 'idlh = '.$data['idlhsua'];
		$query = $this->_db->update($this->_lh, $arr, $where);
	return $query;	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :xoa all lien he
	 * @param unknown_type $mangid
	 */
	public function deletealllh($mangid)
	{
	    $arrayid = explode(',', $mangid);
    	for ($i =0;$i< count($arrayid);$i++){
    		$idlh = $arrayid[$i];
    		//sql xóa
    		$where = 'idlh = '.$idlh;
    		$xoa = $this->_db->delete($this->_lh,$where);
    		
    	}
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : thực hiện tìm 
	 * @param unknown_type $uid
	 * @param unknown_type $nhom
	 */
	public function getalllienhethemnhom($uid,$nhom)
	{
		$sql = $this->_db->select()
						 ->from($this->_lh)
						 ->joinLeft($this->_nlh, $this->_lh.'.id_nlh = '.$this->_nlh.'.id_nlh','ten_nhom_lh')
						 ->where($this->_lh.'.uid = ?',$uid)
						 ->where($this->_lh.'.id_nlh = ?',$nhom)
						 ->order('idlh  DESC');
			$query = $this->_db->fetchAll($sql);
		return $query;	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here ...
	 * @param unknown_type $uid
	 * @param unknown_type $key
	 */
	public function getalllienhetimkiem($uid,$key)
	{
		
		$sql = $this->_db->select()
						 ->from($this->_lh)
						 ->joinLeft($this->_nlh, $this->_lh.'.id_nlh = '.$this->_nlh.'.id_nlh','ten_nhom_lh')
						 ->where($this->_lh.'.uid = ?',$uid)
						  ->where('tenlh LIKE ?',$key.'%')
						 ->order('idlh  DESC');
			$query = $this->_db->fetchAll($sql);
		return $query;		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : lấy toàn bộ lien hệ theo thừ khóa tìm kiếm 
	 * @param unknown_type $data
	 */
	public function gettimkiemlienhe($data,$key)
	{
		
		$sql = $this->_db->select()
						 ->from($this->_lh)
						 ->where('uid = ?',$data['iddn'])
						 ->where('tenlh LIKE ?',$key.'%')
						 ->limit('10');
						 
		$query = $this->_db->fetchAll($sql);
	return $query;				 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description Xóa tất cả các liên hệ khi xóa nhóm
	 */	
	public function xoalienhetheonhom($idnhom)
	{
		$where = "id_nlh = ".$idnhom;
		$sql = $this->_db->delete($this->_lh,$where);
		return  $sql;
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : thực hiện tìm theo cả nhóm và tên 
	 * @param unknown_type $uid
	 * @param unknown_type $nhom
	 */
	public function getalllienhethemnhomkeytim($uid,$nhom,$keytim)
	{
		$sql = $this->_db->select()
						 ->from($this->_lh)
						 ->joinLeft($this->_nlh, $this->_lh.'.id_nlh = '.$this->_nlh.'.id_nlh','ten_nhom_lh')
						 ->where($this->_lh.'.uid = ?',$uid)
						 ->where($this->_lh.'.id_nlh = ?',$nhom)
						 ->where('tenlh LIKE ?',$keytim.'%')
						 ->order('idlh  DESC');
			$query = $this->_db->fetchAll($sql);
		return $query;	
	}	
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description : tinh tong xem co bao nhieu lien he
	 */
	public function demlienhe($nhonlh)
	{
		//$sql = "SELECT COUNT(*) FROM lien_he WHERE id_nlh = ?".$nhonlh;
		$sql = $this->_db->select()
					     ->from($this->_lh,array('COUNT(*) AS tong'))
						 ->where('id_nlh =?',$nhonlh );
						 //echo $sql;
		$query = $this->_db->fetchOne($sql);

		
	return $query;
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : sql update trang thai lien he
	 * @param unknown_type $data
	 */
	public  function updatetrangthailienhe($data)
	{	
		$date = date('m-d-Y H:i:s');
		$array = array(
				'flag'			=>$data['thongso'],
				'ngay_sua'		=>$date,
		);
		$where = 'idlh = '.$data['idlh'];
	$query = $this->_db->update($this->_lh, $array,$where);
	
	return  $query;
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : get all lien he quan trong  theo id  
	 * @param unknown_type $uid
	 */
	public function getalllienheghichu($uid){
		$sql = $this->_db->select()
						 ->from($this->_lh)
						 ->join($this->_nlh, $this->_nlh.'.id_nlh = '.$this->_lh.'.id_nlh','ten_nhom_lh')
						 ->where($this->_lh.'.uid ='.$uid)
						 ->where($this->_lh.'.flag = 1')
						 ->limit(15);
		$query = $this->_db->fetchAll($sql);
	return $query;				 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : check trung email 
	 * @param unknown_type $data
	 */
	public function checktrunglienhe($data)
	{
		$sql = $this->_db->select()
						 ->from($this->_lh,array('COUNT(*) AS tong'))
						 ->where('email = "'.$data['email'].'"')
						 ->where('uid ='.$data['uid']);
		return  $this->_db->fetchOne($sql);
						 
	}
}